package com.hankarun.gevrek;

import com.hankarun.gevrek.lib.ServerManager;

import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;

public class LoginActivity extends Activity {
	public static final String PREFS_NAME = "GevrekPrefs";
	
	private String username;
	private String password;
	private boolean savepass;
	
	private TextView usernameedit;
	private TextView passwordedit;
	private CheckBox savepassword;
	private Button loginbutton;
	
	protected ServerManager app;
	
	private void getLayouts(){
		usernameedit = (TextView) findViewById(R.id.loginnameEdit);
		passwordedit = (TextView) findViewById(R.id.loginpassEdit);
		savepassword = (CheckBox) findViewById(R.id.savePassCheckBox);
		loginbutton = (Button) findViewById(R.id.loginButton);
	}
	
	private void getPreferences(){
        SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
        username = settings.getString("username", "");
        password = settings.getString("password", "");
        savepass = settings.getBoolean("savepass", false);
        
        usernameedit.setText(username);
        passwordedit.setText(password);
        savepassword.setChecked(savepass);
	}

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        
        getLayouts();
        getPreferences();

        loginbutton.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				username = usernameedit.getText().toString();
				password = passwordedit.getText().toString();
				savepass = savepassword.isChecked();
				
            	app = (ServerManager)getApplication();
            	ServerTask task = new ServerTask();
        		task.execute();								
			}
        });        
    }
    
    private class ServerTask extends AsyncTask<Void, Void, Integer> {
		private ProgressDialog dialog;
		
		@Override
		protected void onPreExecute(){
			dialog = ProgressDialog.show(LoginActivity.this, "", 
                    getString(R.string.loginin), true);
		}
		
		@Override
		protected void onPostExecute(Integer result) {
  	      	dialog.dismiss();
			if(result == 0){
				SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
			    SharedPreferences.Editor editor = settings.edit();
			    editor.putString("username", username);
			    if(savepass){
			    	editor.putString("password",password);
			    	editor.putBoolean("savepass", savepass);			    	
			    }else{
			    	editor.putString("password","");
			    	editor.putBoolean("savepass", savepass);
			    }
			    editor.commit();
			    Toast.makeText(getApplicationContext(), R.string.loginsuccess, Toast.LENGTH_SHORT).show();
		    	Intent myIntent = new Intent(LoginActivity.this, GroupActivity.class);
		    	LoginActivity.this.startActivityForResult(myIntent, 0);
			}else{
				if(result == -2)
					Toast.makeText(getApplicationContext(), R.string.loginfailure , Toast.LENGTH_SHORT).show();
				if(result == -1)
					Toast.makeText(getApplicationContext(), R.string.connectionerror , Toast.LENGTH_SHORT).show();
			}
		}

		@Override
		protected Integer doInBackground(Void... params) {
			app.setAuth(username, password);
        	return app.connect();
		}
	}
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
    	if(resultCode == 0)
    	{
    		
    	}
    }
}
